package com.funong.matrix.biz.base.dao;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.funong.matrix.biz.base.entity.SysRolePermissionEntity;
import com.funong.matrix.biz.base.mapper.SysRolePermissionMapper;
import com.funong.matrix.biz.power.pojo.dto.SysRolePermissionDTO;
import com.funong.matrix.core.base.Dao;
import com.funong.matrix.core.base.FunongBaseDao;
import com.funong.matrix.core.enums.IsDeletedEnum;
import com.funong.matrix.core.utils.DateTimeUtil;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

@Dao
@Aspect
public class SysRolePermissionDao extends FunongBaseDao<SysRolePermissionMapper, SysRolePermissionEntity> {

    public List<SysRolePermissionEntity> queryRolePermissionByRoleCode(String roleCode) {
        return list(Wrappers.<SysRolePermissionEntity>lambdaQuery().eq(SysRolePermissionEntity::getRoleCode, roleCode)
                .eq(SysRolePermissionEntity::getIsDeleted,IsDeletedEnum.NOT_DELETED.getValue()));
    }

    public List<SysRolePermissionEntity> queryPermissionListByRoleCodeList(List<String> roleCodeList) {
        return list(Wrappers.<SysRolePermissionEntity>lambdaQuery().in(SysRolePermissionEntity::getRoleCode, roleCodeList)
                .eq(SysRolePermissionEntity::getIsDeleted,IsDeletedEnum.NOT_DELETED.getValue()));
    }

    public void deleteRolePermission(SysRolePermissionDTO rolePermissionDTO) {
        remove(Wrappers.<SysRolePermissionEntity>lambdaUpdate().eq(SysRolePermissionEntity::getRoleCode, rolePermissionDTO.getRoleCode()));
    }

    /**
     * 根 据角色编码删除
     *
     * @param roleCode
     */
    public void deleteRolePermission(String roleCode) {
        new LambdaUpdateChainWrapper<>(this.baseMapper)
                .eq(SysRolePermissionEntity::getRoleCode, roleCode).remove();
    }

    /**
     * 根据角色编码保存
     *
     * @param roleCode
     * @param permissionCodeList
     */
    public void saveRolePermission(String roleCode, List<String> permissionCodeList) {
        if (CollectionUtils.isNotEmpty(permissionCodeList)) {
            for (String code : permissionCodeList) {
                SysRolePermissionEntity entity = new SysRolePermissionEntity().setRoleCode(roleCode).setPermissionCode(code);
                entity.setCreatedAt(DateTimeUtil.now());
                entity.setUpdatedAt(DateTimeUtil.now());
                this.save(entity);
            }
        }
    }

}